//
//  WATFSettingViewController.swift
//  WiFiFaster
//
//  Created by one on 2023/12/28.
//

import UIKit

class WATFSettingViewController: WATFBaseCollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    // MARK: - watf action
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        watf_collectionView.reloadData()
    }
    override func watf_layout() -> UICollectionViewFlowLayout {
        let watf_layout = UICollectionViewFlowLayout()
        watf_layout.itemSize = CGSize(width: watf_SCREEN_WIDTH-64, height: 56)
        watf_layout.minimumLineSpacing = 12
        watf_layout.headerReferenceSize = CGSize(width: watf_SCREEN_WIDTH-40, height: 173)
        return watf_layout
    }
    override func watf_collectionViews() {
        super.watf_collectionViews()
        view.addSubview(watf_navView)
        view.addSubview(watf_iconView)
        watf_iconView.snp.makeConstraints { make in
            make.size.equalTo(100)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(watf_collectionView.snp.top).offset(22)
        }
        watf_collectionView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: watf_SCREEN_WIDTH-40, height: 385))
            make.centerX.equalToSuperview()
            make.top.equalTo(watf_navView.snp.bottom).offset(120)
        }
        watf_collectionView.backgroundColor = WATFCommon.watf_getHXColorValue(rgb: 0x322E2B)
        watf_collectionView.layer.cornerRadius = 16
        watf_collectionView.register(WATFSettingCell.classForCoder(), forCellWithReuseIdentifier: WATFSettingCell.watf_viewToString)
        watf_collectionView.register(WATFSettingHeadView.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: WATFSettingHeadView.watf_viewToString)
        watf_collectionView.dataSource = self
        watf_collectionView.delegate = self
    }
    // MARK: - watf lazy
    lazy var watf_navView: WATFNavView = {
        let watf_navView = WATFNavView()
        watf_navView.watf_leftBlock = {[weak self] in
            self?.navigationController?.popToRootViewController(animated: true)
        }
        return watf_navView
    }()
    lazy var watf_models: [WATFSettingModel] = {
        var watf_models = [WATFSettingModel]()
        watf_models.append(WATFSettingModel(watf_title: "Privacy Policy"))
        watf_models.append(WATFSettingModel(watf_title: "Rate Us", watf_type: .watf_rate))
        watf_models.append(WATFSettingModel(watf_title: "Share Us", watf_type: .watf_share))
        return watf_models
    }()
    lazy var watf_iconView: UIImageView = {
        let watf_iconView = UIImageView(image: UIImage(named: "watf_setting_icon"))
        return watf_iconView
    }()
}
extension WATFSettingViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return watf_models.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let watf_cell = collectionView.dequeueReusableCell(withReuseIdentifier: WATFSettingCell.watf_viewToString, for: indexPath) as! WATFSettingCell
        let watf_model = watf_models[indexPath.row]
        watf_cell.watf_titleLabel.text = watf_model.watf_title
        return watf_cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let watf_headView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: WATFSettingHeadView.watf_viewToString, for: indexPath) as! WATFSettingHeadView
        watf_headView.watf_updateStateView()
        return watf_headView
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let watf_model = watf_models[indexPath.row]
        if watf_model.watf_type == .watf_privacy {
            navigationController?.pushViewController(WATFPrivacyViewController(), animated: true)
            return
        }
        if watf_model.watf_type == .watf_share {
            WATFManager.watf_manager.watf_shareApp(watf_vc: self)
            return
        }
        WATFManager.watf_manager.watf_switchAppStore()
    }
}
